﻿#include "handler.h"

#include <QDebug>
#include <string>

#include "operatornode.h"
#include "valuenode.h"
#include "sentencenode.h"

using namespace std;

Handler::Handler()
{

}

void Handler::setInput(QString input)
{
    m_input = input;
}

void Handler::handle()
{
    AbstractNode      *left = NULL;
    AbstractNode     *right = NULL;
    AbstractNode        *op = NULL;
    AbstractNode  *sentence = NULL;

    QString input = m_input;
    QStringList inputList = m_input.split(" ");

    for (int i = 0; i < inputList.size() - 2; i++) {
        left = new ValueNode((uint8_t)*inputList[i].toStdString().c_str());
        op = new OperatorNode(inputList[i + 1]);
        right = new ValueNode((uint8_t)*inputList[i + 2].toStdString().c_str());
        sentence = new SentenceNode(left, op, right);
        inputList[i + 2] = QString(1, sentence->interpret());
    }

    m_result = inputList[inputList.size() - 1];

    this->output();

}

void Handler::output()
{
    qDebug() << m_result;
}
